﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using BitmapTagger;

namespace ImageTagEditor
{
	/// <summary>
	/// Interaction logic for ImageTagOptionsDialog.xaml
	/// </summary>
	public partial class ImageTagOptionsDialog : Window
	{
		/// <summary>
		/// Create a new Image Tag Options Dialog
		/// </summary>
		public ImageTagOptionsDialog()
		{
			InitializeComponent();
			CheckPrecedenceRadioButton();
		}

		#region save properties

		/// <summary>
		/// Get or set whether the Exif tag system is saved
		/// </summary>
		public bool SaveExif
		{
			get { return (bool)GetValue(SaveExifProperty); }
			set { SetValue(SaveExifProperty, value); }
		}

		/// <summary>
		/// Identifies the SaveExif dependency property
		/// </summary>
		public static readonly DependencyProperty SaveExifProperty =
			DependencyProperty.Register("SaveExif", typeof(bool), 
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		/// <summary>
		/// Get or set whether the XMP Exif tag system is saved
		/// </summary>
		public bool SaveXMPExif
		{
			get { return (bool)GetValue(SaveXMPExifProperty); }
			set { SetValue(SaveXMPExifProperty, value); }
		}

		/// <summary>
		/// Identifies the SaveXMPExif dependency property
		/// </summary>
		public static readonly DependencyProperty SaveXMPExifProperty =
			DependencyProperty.Register("SaveXMPExif", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		/// <summary>
		/// Get or set whether the XMP  tag system is saved
		/// </summary>
		public bool SaveXMP
		{
			get { return (bool)GetValue(SaveXMPProperty); }
			set { SetValue(SaveXMPProperty, value); }
		}

		/// <summary>
		/// Identifies the SaveXMP dependency property
		/// </summary>
		public static readonly DependencyProperty SaveXMPProperty =
			DependencyProperty.Register("SaveXMP", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		#endregion

		#region load properties

		/// <summary>
		/// Get or set whether the Exif tag system is loaded
		/// </summary>
		public bool LoadExif
		{
			get { return (bool)GetValue(LoadExifProperty); }
			set { SetValue(LoadExifProperty, value); }
		}

		/// <summary>
		/// Identifies the LoadExif dependency property
		/// </summary>
		public static readonly DependencyProperty LoadExifProperty =
			DependencyProperty.Register("LoadExif", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));


		/// <summary>
		/// Get or set whether the XMP Exif tag system is loaded
		/// </summary>
		public bool LoadXMPExif
		{
			get { return (bool)GetValue(LoadXMPExifProperty); }
			set { SetValue(LoadXMPExifProperty, value); }
		}

		/// <summary>
		/// Identifies the LoadXMPExif dependency property
		/// </summary>
		public static readonly DependencyProperty LoadXMPExifProperty =
			DependencyProperty.Register("LoadXMPExif", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		/// <summary>
		/// Get or set whether the XMP  tag system is loaded
		/// </summary>
		public bool LoadXMP
		{
			get { return (bool)GetValue(LoadXMPProperty); }
			set { SetValue(LoadXMPProperty, value); }
		}

		/// <summary>
		/// Identifies the LoadXMP dependency property
		/// </summary>
		public static readonly DependencyProperty LoadXMPProperty =
			DependencyProperty.Register("LoadXMP", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		#endregion


		#region tag precedence

		/// <summary>
		/// Get or set the tag system precedence
		/// </summary>
		public TagPrecedence Precedence
		{
			get { return (TagPrecedence)GetValue(PrecedenceProperty); }
			set { SetValue(PrecedenceProperty, value); }
		}

		/// <summary>
		/// Identifies the Precedence dependency property
		/// </summary>
		public static readonly DependencyProperty PrecedenceProperty =
			DependencyProperty.Register("Precedence", typeof(TagPrecedence), 
			typeof(ImageTagOptionsDialog),
			new UIPropertyMetadata(TagPrecedence.Exif_XMPExif_XMP, OnPrecedenceChanged));



		/// <summary>
		/// Dependency Property Precedence changed listener
		/// </summary>
		/// <param name="sender">The ImageTagOptionsDialog whose dependency 
		/// property changed</param>
		/// <param name="e">The event args that came with the change</param>
		protected static void OnPrecedenceChanged(object sender, DependencyPropertyChangedEventArgs e)
		{
			(sender as ImageTagOptionsDialog).OnPrecedenceChanged(e);
		}

		/// <summary>
		/// Listens for when the Precedence Dependency Property value changes.
		/// </summary>
		/// <param name="e">Not Used</param>
		protected void OnPrecedenceChanged(DependencyPropertyChangedEventArgs e)
		{
			CheckPrecedenceRadioButton();
		}

		/// <summary>
		/// Checks the precedence radio button for the current precedence value
		/// </summary>
		private void CheckPrecedenceRadioButton()
		{
			if (Precedence == TagPrecedence.Exif_XMP_XMPExif)
				exif_XMP_XMPExifRadioButton.IsChecked = true;
			else if (Precedence == TagPrecedence.Exif_XMPExif_XMP)
				exif_XMPExif_XMPRadioButton.IsChecked = true;
			else if (Precedence == TagPrecedence.XMP_Exif_XMPExif)
				xmp_Exif_XMPExifRadioButton.IsChecked = true;
			else if (Precedence == TagPrecedence.XMP_XMPExif_Exif)
				xmp_XMPExif_ExifRadioButton.IsChecked = true;
			else if (Precedence == TagPrecedence.XMPExif_Exif_XMP)
				xmpExif_Exif_XMPRadioButton.IsChecked = true;
			else if (Precedence == TagPrecedence.XMPExif_XMP_Exif)
				xmpExif_XMP_ExifRadioButton.IsChecked = true;
			else
				throw new Exception("Unknown Tag Precedence");
		}

		#endregion

		#region shown tags

		/// <summary>
		/// Get or set whether the user comment tag is shown
		/// </summary>
		public bool ShowUserComment
		{
			get { return (bool)GetValue(ShowUserCommentProperty); }
			set { SetValue(ShowUserCommentProperty, value); }
		}

		/// <summary>
		/// Identifies the ShowUserComment dependency property
		/// </summary>
		public static readonly DependencyProperty ShowUserCommentProperty =
			DependencyProperty.Register("ShowUserComment", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));


		/// <summary>
		/// Get or set whether the maker note tag is shown
		/// </summary>
		public bool ShowMakerNote
		{
			get { return (bool)GetValue(ShowMakerNoteProperty); }
			set { SetValue(ShowMakerNoteProperty, value); }
		}

		/// <summary>
		/// Identifies the ShowMakerNote dependency property
		/// </summary>
		public static readonly DependencyProperty ShowMakerNoteProperty =
			DependencyProperty.Register("ShowMakerNote", typeof(bool),
			typeof(ImageTagOptionsDialog), new UIPropertyMetadata(true));

		#endregion



		/// <summary>
		/// Listens for when the ok button is clicked 
		/// and sets dialog result to true
		/// </summary>
		/// <param name="sender">Not Used</param>
		/// <param name="e">Not Used</param>
		private void okButton_Click(object sender, RoutedEventArgs e)
		{
			DialogResult = true;
		}


		/// <summary>
		/// Listens for when the canel button is clicked and 
		/// sets dialog result to false
		/// </summary>
		/// <param name="sender">Not Used</param>
		/// <param name="e">Not Used</param>
		private void cancelButton_Click(object sender, RoutedEventArgs e)
		{
			DialogResult = false;
		}

		private void exif_XMPExif_XMPRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.Exif_XMPExif_XMP;
		}

		private void xmpExif_Exif_XMPRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.XMPExif_Exif_XMP;
		}

		private void xmp_Exif_XMPExifRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.XMP_Exif_XMPExif;
		}

		private void exif_XMP_XMPExifRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.Exif_XMP_XMPExif;
		}

		private void xmpExif_XMP_ExifRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.XMPExif_XMP_Exif;
		}

		private void xmp_XMPExif_ExifRadioButton_Checked(object sender, RoutedEventArgs e)
		{
			Precedence = TagPrecedence.XMP_XMPExif_Exif;
		}
	}
}
